package com.azt.provider.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.pojo.Dictionary;
import com.azt.api.service.DictionaryService;
import com.azt.provider.mapper.DictionaryMapper;

import tk.mybatis.mapper.entity.Example;

/**
 * 字典管理
 * 
 * @author 张栋
 * @version [版本号, 2014-4-11]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service
@Transactional
public class DictionaryServiceImpl implements DictionaryService
{
	
	@Autowired
	private DictionaryMapper dm;
    
    /**
     * 存放中国省份 key:省份code val:省份名称
     */
    private static Map<String, String> PROVINCES = new HashMap<String, String>();
    
    
    /**
     * 重载方法
     * 
     * @param i
     * @return
     */
    @Override
    public List<Dictionary> getDictionByParentId(Integer pid)
    {
        Example exp = new Example(Dictionary.class);
        exp.createCriteria().andEqualTo("parentId", pid);
        exp.setOrderByClause(" sort_num asc , id desc ");
        return dm.selectByExample(exp);
    }
    
    /**
     * 重载方法
     * 
     * @param id
     * @return
     */
    @Override
    public Dictionary getDictionById(Integer id)
    {
        return dm.selectByPrimaryKey(id);
    }
    
    /**
     * 重载方法
     * 
     * @param id
     */
    @Override
    public void deleteAllById(Integer id)
    {
    	dm.deleteByPrimaryKey(id);
    }
    
    /**
     * 重载方法
     * 
     * @param code
     * @return
     */
    @Override
    public Dictionary getDictionByCode(String code)
    {
        Example exp = new Example(Dictionary.class);
        exp.createCriteria().andEqualTo("code", code);
        List<Dictionary> list = dm.selectByExample(exp);
        if (list.size() == 0)
        {
            return null;
        }
        else
        {
            return (Dictionary)list.get(0);
        }
    }
    
    @Override
    public List<Dictionary> getDictionByParentCode(String code)
    {
        return dm.getDicsByPcode(code);
    }

    @Override
    public List<Dictionary> getDictionWithChildByParentCode(String code) {
        return dm.getDicWithChild(code);
    }

    @Override
    public void saveOrUpdate(Dictionary old)
    {
        if(old.getId() == null){
        	dm.insert(old);
        }else{
        	dm.updateByPrimaryKey(old);
        }
    }
    
    @Override
    public void save(Dictionary diction)
    {
        dm.insert(diction);
    }
    
    @Override
    public Map<String, String> getChinaProvinces()
    {
        
        synchronized (PROVINCES)
        {
            if (PROVINCES.isEmpty())
            {
                Example exp = new Example(Dictionary.class);
                exp.createCriteria().andEqualTo("code", "86");
                List<Dictionary> ds = dm.selectByExample(exp);
                if(!ds.isEmpty()){
                	Dictionary china = ds.get(0);
                	Integer chinaid = china.getId();
                	Example expl = new Example(Dictionary.class);
                    expl.createCriteria().andEqualTo("parentId", chinaid);
                    List<Dictionary> provinces = dm.selectByExample(expl);
                	for (int i = 0; i < provinces.size(); i++)
                	{
                		Dictionary province = provinces.get(i);
                		PROVINCES.put("86-" + province.getCode(), province.getName());
                	}
                }
            }
        }
        return PROVINCES;
    }
    
    @Override
    public String findByIds(String ids)
    {
        return dm.findNamesByIds(ids);
    }
    
    @Override
    public String findByCodes(String codes)
    {
        return dm.findNamesByCodes(codes);
    }
    
    @Override
    public String findByCodesAndPid(String codes, String pid)
    {
        return dm.findByCodesAndPid(codes, Integer.parseInt(pid));
    }
    
    @Override
    public boolean findOrtherDiction(Dictionary diction)
    {
        List<Dictionary> dictionarys = null;
        Integer id = diction.getId();
        String code = diction.getCode();
        Integer parentid = diction.getParentId();
        if (id != null && !"".equals(id))
        {
            dictionarys = dm.findBycodeAndNotId(code, id, parentid);
        }
        else
        {
            Example exp = new Example(Dictionary.class);
            exp.createCriteria().andEqualTo("code", code).andEqualTo("parentId",parentid);
            dictionarys = dm.selectByExample(exp);
        }
        if (dictionarys.size() > 0)
        {
            return true;
        }
        
        return false;
    }
}
